<?php

namespace Home\Controller;

use Admin\Controller\CommonController;
use Think\Controller;

class CartController extends CommoController
{

    //  添加商品到购物车

    public function adCart()
    {
        $model_add_cart = D('cart');
        $data['goods_id'] = $_POST['goods_id'];
        $member = session('member');
        $data['h_admin_id'] = $member[0]['id'];
        if ($model_add_cart->add($data)) {
            $this->ajaxRet(self::RET_FAILED);
        } else {
            $this->ajaxRet(self::RET_FAILED);
        }
        return;
    }

    //查询购物车商品
    public function cart()
    {
        $model_cart = D('cart');
        $member = session('member');
        $member_id = $member[0]['id'];

        $res = $model_cart->query("SELECT g.id, g.goods_title, g.goods_pic, g.goods_price 
                              FROM goods AS g JOIN cart AS c 
                              ON g.id = c.goods_id WHERE h_admin_id = {$member_id}");
        $this->assign('lst', $res);

//        $res = $sel->query("select * from cart where h_admin_id = '$e'");
//        $lst = array();
//        foreach ($res as $k => $v) {
//            $v['goods_id'];
//            $tmp = $sel->query("select * from goods where id = " . $v['goods_id'] . " ");
//            array_push($lst, $tmp[0]);
//            $a = 1;
//        }
//        $this->assign('lst', $lst);
        $this->display();
    }

    //删除购物车里的商品
    public function del()
    {
        $id = $_POST['id'];
        $model_del = D('cart');
        $member = session('member');
        $member_id = $member[0]['id'];
        $res = $model_del->where("h_admin_id = {$member_id} AND goods_id = {$id} ")->delete();

    }

}